package com.ruoyi.operator.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.operator.domain.OptShopCategory;
import com.ruoyi.operator.service.IOptShopCategoryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家类目Controller
 *
 * @date 2021-02-08
 */
@Controller
@RequestMapping("/operator/category")
public class OptShopCategoryController extends BaseController
{
    private String prefix = "operator/category";

    @Autowired
    private IOptShopCategoryService optShopCategoryService;

    @RequiresPermissions("operator:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询商家类目列表
     */
    @RequiresPermissions("operator:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OptShopCategory optShopCategory)
    {
        startPage();
        optShopCategory.setIsDelete(0);
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        optShopCategory.setDeptId(String.valueOf(currentUser.getDeptId()));
        List<OptShopCategory> list = optShopCategoryService.selectOptShopCategoryList(optShopCategory);
        return getDataTable(list);
    }

    /**
     * 导出商家类目列表
     */
    @RequiresPermissions("operator:category:export")
    @Log(title = "商家类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OptShopCategory optShopCategory)
    {
        List<OptShopCategory> list = optShopCategoryService.selectOptShopCategoryList(optShopCategory);
        ExcelUtil<OptShopCategory> util = new ExcelUtil<OptShopCategory>(OptShopCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增商家类目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商家类目
     */
    @RequiresPermissions("operator:category:add")
    @Log(title = "商家类目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OptShopCategory optShopCategory)
    {
        return toAjax(optShopCategoryService.insertOptShopCategory(optShopCategory));
    }

    /**
     * 修改商家类目
     */
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        OptShopCategory optShopCategory = optShopCategoryService.selectOptShopCategoryById(categoryId);
        mmap.put("optShopCategory", optShopCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存商家类目
     */
    @RequiresPermissions("operator:category:edit")
    @Log(title = "商家类目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OptShopCategory optShopCategory)
    {
        return toAjax(optShopCategoryService.updateOptShopCategory(optShopCategory));
    }

    /**
     * 删除商家类目
     */
    @RequiresPermissions("operator:category:remove")
    @Log(title = "商家类目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(optShopCategoryService.deleteOptShopCategoryByIds(ids));
    }

    /**
     * 类目状态修改
     */
    @RequiresPermissions("operator:category:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(OptShopCategory optShopCategory)
    {
        return toAjax(optShopCategoryService.changeStatus(optShopCategory));
    }
}
